import QtQuick 2.9

Item {
    id: root

    enum MouseType {
        Unknown = 0,
        Top = 1,
        Bottom = 2,
        Left = 3,
        Right = 4,
        TopLeft = 5,
        TopRight = 6,
        BottomLeft = 7,
        BottomRight = 8
    }

    property int mouseType: RectSelector.MouseType.Unknown
    property int mouseBorderWidth: 10   //可检测鼠标的边框区域宽度
    property color color: "#017ff6"     //主色 边框和指示点的颜色

    Rectangle{
        id: _selectRect
        anchors.fill: parent
        anchors.margins: mouseBorderWidth / 2
        color: "transparent"
        border.color: root.color
        border.width: 2

        MouseArea{
            id: _mouseArea

            property int range: 10

            property real lastX: 0
            property real lastY: 0
            property real moveX: 0
            property real moveY: 0

            anchors.fill: parent
            cursorShape: Qt.SizeAllCursor
            drag.target: cursorShape === Qt.SizeAllCursor ? root : null
            hoverEnabled: true

            onPressedChanged: {
                if(containsPress){
                    lastX = mouseX;
                    lastY = mouseY;
                }
                console.log("pressed ch:",pressed)
            }
            onPositionChanged: {
                console.log("move:",moveX,moveY,mouseX,mouseY)
                //鼠标未按下可修改鼠标指针形状 按下不可修改
                if(!pressed)
                {
                    if(mouseY < range){                     //上
                        if(mouseX < range){                 //左上
                            cursorShape = Qt.SizeFDiagCursor
                            mouseType = 5
                        }else if(mouseX > (width - range)){ //右上
                            cursorShape = Qt.SizeBDiagCursor
                            mouseType = RectSelector.MouseType.TopRight
                        }else{                              //中上
                            cursorShape = Qt.SizeVerCursor
                            mouseType = RectSelector.MouseType.Top
                        }
                    }else if(mouseY > (height - range)){    //下
                        if(mouseX < range){                 //左下
                            cursorShape = Qt.SizeBDiagCursor
                            mouseType = RectSelector.MouseType.BottomLeft
                        }else if(mouseX > (width - range)){ //右下
                            cursorShape = Qt.SizeFDiagCursor
                            mouseType = RectSelector.MouseType.BottomRight
                        }else{                              //中下
                            cursorShape = Qt.SizeVerCursor
                            mouseType = RectSelector.MouseType.Bottom
                        }
                    }else if(mouseX < range){               //左
                        cursorShape = Qt.SizeHorCursor
                        mouseType = RectSelector.MouseType.Left

                    }else if(mouseX > (width - range)){     //右
                        cursorShape = Qt.SizeHorCursor
                        mouseType = RectSelector.MouseType.Right

                    }else{
                        cursorShape = Qt.SizeAllCursor
                        mouseType = RectSelector.MouseType.Unknown
                    }

                }
                else
                {
                    moveX = mouseX - lastX;
                    moveY = mouseY - lastY;

                    switch(root.mouseType){
                    case RectSelector.MouseType.Unknown:{}break;
                    case RectSelector.MouseType.Top:{
                        root.y += moveY
                        root.height -= moveY
                    }break;
                    case RectSelector.MouseType.Bottom:{
                        root.height += moveY
                    }break;
                    case RectSelector.MouseType.Left:{
                        root.x += moveX
                        root.width -= moveX
                    }break;
                    case RectSelector.MouseType.Right:{
                        root.width += moveX
                    }break;
                    case RectSelector.MouseType.TopLeft:{
                        root.y += moveY
                        root.height -= moveY
                        root.x += moveX
                        root.width -= moveX
                    }break;
                    case RectSelector.MouseType.TopRight:{
                        root.y += moveY
                        root.height -= moveY
                        root.width += moveX
                    }break;
                    case RectSelector.MouseType.BottomLeft:{
                        root.height += moveY
                        root.x += moveX
                        root.width -= moveX
                    }break;
                    case RectSelector.MouseType.BottomRight:{
                        root.height += moveY
                        root.width += moveX
                    }break;
                    default: break;
                    }

                    lastX = mouseX;
                    lastY = mouseY;
                }
            }
        }
    }


    Component{
        id: _indicationPoint
        Rectangle{
            visible: !_mouseArea.pressed
            width: mouseBorderWidth
            height: mouseBorderWidth
            color: root.color
            border.color: "white"
            border.width: 1
            z: 10
        }
    }

    //上下左右
    Loader{
        anchors.top: root.top
        anchors.horizontalCenter: root.horizontalCenter
        sourceComponent: _indicationPoint
    }
    Loader{
        anchors.bottom: root.bottom
        anchors.horizontalCenter: root.horizontalCenter
        sourceComponent: _indicationPoint
    }
    Loader{
        anchors.left: root.left
        anchors.verticalCenter: root.verticalCenter
        sourceComponent: _indicationPoint
    }
    Loader{
        anchors.right: root.right
        anchors.verticalCenter: root.verticalCenter
        sourceComponent: _indicationPoint
    }

    //四角
    Loader{
        anchors.top: root.top
        anchors.left: root.left
        sourceComponent: _indicationPoint
    }
    Loader{
        anchors.top: root.top
        anchors.right: root.right
        sourceComponent: _indicationPoint
    }
    Loader{
        anchors.bottom: root.bottom
        anchors.left: root.left
        sourceComponent: _indicationPoint
    }
    Loader{
        anchors.bottom: root.bottom
        anchors.right: root.right
        sourceComponent: _indicationPoint
    }
}
